package com.example.swipytabs;



import android.app.ListFragment;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Tab1Fragment extends Fragment implements OnItemClickListener {
    public Tab1Fragment() {
    }

    ListView listViewMedia; 
    String[] pinyinArray, chineseCharacters;
    MediaPlayer mPlayer = new MediaPlayer();
    TextView textViewStatus;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View v = inflater.inflate(R.layout.tab1layout, null);
    	
    	//get string array with Chinese characters
    	String[] chineseCharacters = getResources().getStringArray(R.array.chinesecharacters1_1);
    	//get string array with pinyin characters
    	pinyinArray = getResources().getStringArray(R.array.pinyin1_1);
    	
    	listViewMedia=(ListView)v.findViewById(R.id.listView1);
    	listViewMedia.setAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(),
    				android.R.layout.simple_list_item_multiple_choice,
    				chineseCharacters));
	
    	listViewMedia.setOnItemClickListener(this);
    	
    	//text field to display character data at the top of the screen
    	textViewStatus=(TextView)v.findViewById(R.id.textViewStatus);
    	
    	//TextView textView = new TextView(getActivity());
        //textView.setGravity(Gravity.CENTER);
        //Bundle args = getArguments();
        //textView.setText("I'm in Tab1Fragment Now!!!!");
        return v;
    }


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int listPosition, long arg3) {
		// TODO Auto-generated method stub
		//Toast.makeText(getActivity(), Integer.toString(listPosition)+"Clicked", Toast.LENGTH_SHORT ).show(); 
		if (mPlayer.isPlaying() == true){
			Toast.makeText(getActivity(), "Stopping "+(CharSequence) listViewMedia.getItemAtPosition(listPosition), Toast.LENGTH_SHORT).show();
			textViewStatus.setText("Stopping "+(CharSequence) listViewMedia.getItemAtPosition(listPosition));
			mPlayer.stop();
			//mPlayer.release();
			if (listPosition > 0)
				playListItem( listPosition);
		}
		else {
			//Toast.makeText(getApplicationContext(), "Starting "+(CharSequence) listViewMedia.getItemAtPosition(listPosition), Toast.LENGTH_SHORT).show();
			textViewStatus.setText(pinyinArray[listPosition].toString());
			playListItem( listPosition);
		}
	}
	
	private void playListItem(int listPosition) {
		// TODO Auto-generated method stub
		int mediaID;
		switch (listPosition % 43)
		{
			case 0:
				mediaID = R.raw.bu;
				break;		
			case 1:
				mediaID = R.raw.bucuo;
				break;
			case 2:
				mediaID = R.raw.hai;
				break;
			case 3:
				mediaID = R.raw.ke;
				break;
			case 4:
				mediaID = R.raw.yi;
				break;
			case 5:
				mediaID = R.raw.mamahuhu;
				break;
			case 6:
				mediaID = R.raw.ma;
				break;
			case 7:
				mediaID = R.raw.hu;
				break;
			case 8:
				mediaID = R.raw.xiexie;
				break;
			case 9:
				mediaID = R.raw.xie;
				break;
			case 10:
				mediaID = R.raw.ne;
				break;		
			case 11:
				mediaID = R.raw.nine;
				break;
			case 12:
				mediaID = R.raw.haikeyi;
				break;
			case 13:
				mediaID = R.raw.nijaioshemamingza;
				break;
			case 14:
				mediaID = R.raw.jiao;
				break;
			case 15:
				mediaID = R.raw.shenme;
				break;
			case 16:
				mediaID = R.raw.mingzi;
				break;				
			case 17:
				mediaID = R.raw.zi;
				break;
			case 18:
				mediaID = R.raw.ming;
				break;
			case 19:
				mediaID = R.raw.wo;
				break;
			case 20:
				mediaID = R.raw.wojiao;
				break;	
			case 21:
				mediaID = R.raw.zai;
				break;		
			case 22:
				mediaID = R.raw.na;
				break;
			case 23:
				mediaID = R.raw.naer1;
				break;
			case 24:
				mediaID = R.raw.nali1;
				break;
			case 25:
				mediaID = R.raw.nali2;
				break;
			case 26:
				mediaID = R.raw.naer2;
				break;
			case 27:
				mediaID = R.raw.xishoujianzainali;
				break;
			case 28:
				mediaID = R.raw.xi;
				break;
			case 29:
				mediaID = R.raw.shou;
				break;
			case 30:
				mediaID = R.raw.jian;
				break;
			case 31:
				mediaID = R.raw.fangjian;
				break;		
			case 32:
				mediaID = R.raw.fangzi;
				break;
			case 33:
				mediaID = R.raw.nijisui;
				break;
			case 34:
				mediaID = R.raw.wushiwushisui;
				break;
			case 35:
				mediaID = R.raw.sui;
				break;
			case 36:
				mediaID = R.raw.hui;
				break;
			case 37:
				mediaID = R.raw.buhui;
				break;
			case 38:
				mediaID = R.raw.nihuishuoyingyuma;
				break;
			case 39:
				mediaID = R.raw.nizhuzainalie;
				break;
			case 40:
				mediaID = R.raw.dian;
				break;	
			case 41:
				mediaID = R.raw.diandian;
				break;	
			case 42:
				mediaID = R.raw.woshouyidiandian;
				break;	
		
			default:
				//textViewStatus.setText("Stopping Play ");
				return;
				//break;
		}
		
		mPlayer = MediaPlayer.create(getActivity(), mediaID);
		mPlayer.start();
	}
}
